home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / lyrics / LyrcParser.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.5 KB  |  63 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import urllib
  5. import re
  6. import rb
  7. EXPS = [
  8.     '\n',
  9.     '\r',
  10.     '<[iI][mM][gG][^>]*>',
  11.     '<[aA][^>]*>[^<]*<\\/[aA]>',
  12.     '<[sS][cC][rR][iI][pP][tT][^>]*>[^<]*(<!--[^>]*>)*[^<]*<\\/[sS][cC][rR][iI][pP][tT]>',
  13.     '<[sS][tT][yY][lL][eE][^>]*>[^<]*(<!--[^>]*>)*[^<]*<\\/[sS][tT][yY][lL][eE]>']
  14. CEXPS = [ re.compile(exp) for exp in EXPS ]
  15. SEPARATOR_RE = re.compile("<[fF][oO][nN][tT][ ]*[sS][iI][zZ][eE][ ]*='2'[ ]*>")
  16.  
  17. class LyrcParser(object):
  18.     
  19.     def __init__(self, artist, title):
  20.         self.artist = artist
  21.         self.title = title
  22.  
  23.     
  24.     def search(self, callback, *data):
  25.         path = 'http://www.lyrc.com.ar/en/'
  26.         wartist = urllib.quote(self.artist)
  27.         wtitle = urllib.quote(self.title)
  28.         wurl = 'tema1en.php?artist=%s&songname=%s' % (wartist, wtitle)
  29.         loader = rb.Loader()
  30.         loader.get_url(path + wurl, self.got_lyrics, callback, *data)
  31.  
  32.     
  33.     def got_lyrics(self, lyrics, callback, *data):
  34.         if lyrics is None:
  35.             callback(None, *data)
  36.             return None
  37.         for exp in CEXPS:
  38.             lyrics = exp.sub('', lyrics)
  39.         
  40.         lyricIndex = SEPARATOR_RE.search(lyrics)
  41.         if lyricIndex is not None:
  42.             callback(self.parse_lyrics(SEPARATOR_RE.split(lyrics, 1)[1]), *data)
  43.         else:
  44.             callback(None, *data)
  45.  
  46.     
  47.     def parse_lyrics(self, lyrics):
  48.         if re.search('<p><hr', lyrics):
  49.             lyrics = re.split('<p><hr', lyrics, 1)[0]
  50.         else:
  51.             lyrics = re.split('<br><br>', lyrics, 1)[0]
  52.         lyrics = re.sub('<[fF][oO][nN][tT][^>]*>', '', lyrics)
  53.         title = re.split('(<[bB]>)([^<]*)', lyrics)[2]
  54.         artist = re.split('(<[uU]>)([^<]*)', lyrics)[2]
  55.         lyrics = re.sub('<[bB]>[^<].*<\\/[tT][aA][bB][lL][eE]>', '', lyrics)
  56.         lyrics = re.sub('<[Bb][Rr][^>]*>', '\n', lyrics)
  57.         titl = '%s - %s\n\n' % (artist, title)
  58.         lyrics = titl + lyrics
  59.         lyrics += '\n\nLyrics provided by lyrc.com.ar'
  60.         return lyrics
  61.  
  62.  
  63.